package com.yh.blog.serviceImpl;

import com.yh.blog.domain.Blog;
import com.yh.blog.domain.Comments;
import com.yh.blog.mapper.BlogMapper;
import com.yh.blog.mapper.CommentsMapper;
import com.yh.blog.service.CommentsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {
    @Resource
    private CommentsMapper commentsMapper;
    @Resource
    private BlogMapper blogMapper;

    //存放迭代找出的所有子代的集合
    private List<Comments> tempReplys = new ArrayList<>();

    @Override
    public List<Comments> listCommentsByBlogId(Integer blogId) {
//  第一步查询顶级评论
        List<Comments> comments=this.commentsMapper.findCommentsByBlogIdAndNotParent(blogId);
//        遍历每一个顶级评论 为了找出每一个的回复
        for (Comments comment : comments) {
            Integer topCommentsId=comment.getCommentsId();
            String parentNickname=comment.getNickname();
//            一级回复的储存集合
            List<Comments> firstReply=this.commentsMapper.findFirstReply(blogId, topCommentsId);
//            查询一级回复
            combineFirstReply(blogId,firstReply,parentNickname);
//            设置顶级评论的回复集合
            comment.setReplyComments(tempReplys);
//            遍历结束将tempReplys初始化
            tempReplys=new ArrayList<>();
        }

        return comments;
    }

    private void combineFirstReply(Integer blodId,List<Comments> firstReply,String parentNickname){
        if(firstReply.size()>0){
            for (Comments firstComment : firstReply) {
                Integer firstCommentsId=firstComment.getCommentsId();
                String firstNickname=firstComment.getNickname();
//                设置一级回复的父昵称
                firstComment.setParentNickname(parentNickname);
                firstComment.setNickname(firstNickname);
                tempReplys.add(firstComment);
//                查询二级回复
                combineSecondReply(blodId,firstCommentsId,firstNickname);
            }
        }
    }

    private void combineSecondReply(Integer blodId,Integer firstCommentsId,String parentNickname){
//      根据一级回复的id找到二级回复
        List<Comments> secondReply=this.commentsMapper.findSecondReply(blodId, firstCommentsId);
        if(secondReply.size()>0){
            for (Comments secondComment : secondReply) {
                Integer secondCommentsId=secondComment.getCommentsId();
                String secondNickname=secondComment.getNickname();
//                设置一级回复的父昵称
                secondComment.setParentNickname(parentNickname);
                secondComment.setNickname(secondNickname);
                tempReplys.add(secondComment);
//                查询二级回复
                combineSecondReply(blodId,secondCommentsId,secondNickname);
            }
        }
    }



    @Override
    public boolean append(Comments comments) {
//        如果parent_commentsId！=-1的话就说明他有父亲
        int parent_commentsId=comments.getParent_commentsId();
        if(parent_commentsId!=-1){
            comments.setParentComment(this.commentsMapper.findByParentsId(parent_commentsId));
        }else{
            comments.setParentComment(null);
        }
        //        增加博客里面的评论数
        Blog blog=this.blogMapper.selectById(comments.getBlog().getBlogId());
        blog.setComment_count(blog.getComment_count()+1);
        this.blogMapper.update(blog);
        return this.commentsMapper.insert(comments);
    }
}
